package org.goj.practice.controller;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.goj.common.entity.practice.Contest;
import org.goj.common.entity.practice.ContestUser;
import org.goj.common.model.Result;
import org.goj.practice.service.ContestService;
import org.goj.practice.service.ContestUserService;
import org.goj.practice.vo.ContestAdminVO;
import org.goj.practice.vo.ContestRankVO;
import org.goj.practice.vo.ContestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 竞赛表 前端控制器
 * </p>
 *
 * @author CK
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/contest")
public class ContestController {

    @Autowired
    private ContestService contestService;

    @Autowired
    private ContestUserService contestUserService;

    /**
     * 管理员查询竞赛列表
     *
     * @param current 当前页
     * @param size    页面大小
     * @param sort    排序规则
     * @param name    搜索名称
     * @return Result<Page < Contest>>
     * @author CK
     * @date 2021/4/19 15:52
     */
    @GetMapping("/contestListAdmin")
    public Result<Page<Contest>> contestListAdmin(
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "sort", required = false, defaultValue = "1") Integer sort,
            @RequestParam(value = "name", required = false) String name) {
        Page<Contest> page = contestService.contestListAdmin(current, size, sort, name);
        return Result.succeed("查询竞赛列表成功", page);
    }

    @PostMapping("/addContest")
    public Result<Boolean> addContest(
            @RequestParam(value = "contestId", required = false, defaultValue = "-1") Integer contestId,
            @RequestParam(value = "userId") Integer userId,
            @RequestParam(value = "title") String title,
            @RequestParam(value = "problemIds") List<Integer> problemIds,
            @RequestParam(value = "startDateTime") String startDateTime,
            @RequestParam(value = "endDateTime") String endDateTime,
            @RequestParam(value = "description") String description,
            @RequestParam(value = "defunct") Boolean defunct,
            @RequestParam(value = "languages") List<Integer> languages,
            @RequestParam(value = "privilege") Integer privilege,
            @RequestParam(value = "password", required = false) String password,
            @RequestParam(value = "numbers", required = false) String numbers) {
        Assert.isTrue(endDateTime.compareTo(startDateTime) > 0, "结束日期需要大于开始日期");
        try {
            contestService.addContest(contestId, userId, title, problemIds, startDateTime, endDateTime, description, defunct, languages, privilege, password, numbers);
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.failed(contestId == -1 ? "添加竞赛&作业失败" : "更新竞赛&作业失败", false);
        }
        return Result.succeed(contestId == -1 ? "添加竞赛&作业成功" : "更新竞赛&作业成功", true);
    }

    /**
     * 根据竞赛ID查询竞赛详情
     *
     * @param contestId 竞赛ID
     * @return Result<ContestAdminVO>
     * @author CK
     * @date 2021/4/20 19:45
     */
    @GetMapping("/contestAdminById")
    public Result<ContestAdminVO> contestAdminById(@RequestParam(value = "contestId") Integer contestId) {
        ContestAdminVO contestAdmin = contestService.contestAdminById(contestId);
        Assert.notNull(contestAdmin, "查询竞赛详情失败");
        return Result.succeed("查询竞赛详情成功", contestAdmin);
    }

    /**
     * 更新竞赛状态
     *
     * @param contestId 竞赛ID
     * @param defunct   状态
     * @return Result<Boolean>
     * @author CK
     * @date 2021/4/20 15:53
     */
    @PostMapping("/updateContestDefunct")
    public Result<Boolean> updateContestDefunct(
            @RequestParam(value = "contestId") Integer contestId,
            @RequestParam(value = "defunct") String defunct) {
        defunct = "N".equals(defunct) ? "Y" : "N";
        Contest contest = new Contest();
        contest.setContestId(contestId).setDefunct(defunct);
        boolean flag = contestService.updateById(contest);
        Assert.isTrue(flag, "修改状态失败！");
        return Result.succeed("修改状态成功", flag);
    }


    /**
     * 管理员删除竞赛或作业
     *
     * @param contestId 竞赛ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/4/20 16:53
     */
    @PostMapping("/deleteContest")
    public Result<Boolean> deleteContest(@RequestParam(value = "contestId") Integer contestId) {
        boolean flag = contestService.removeById(contestId);
        Assert.isTrue(flag, "删除竞赛失败");
        return Result.succeed("删除竞赛成功", flag);
    }

    /**
     * 用户条件查询竞赛列表
     *
     * @param current           当前页
     * @param size              分页大小
     * @param userId            用户ID
     * @param name              查询标题
     * @param selectStatusValue 权限
     * @param selectClassValue  分类状态
     * @return Result<Page < ContestVO>>
     * @author CK
     * @date 2021/4/21 10:47
     */
    @GetMapping("/pageContest")
    public Result<Page<ContestVO>> pageContest(
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "selectStatusValue", required = false, defaultValue = "-1") Integer selectStatusValue,
            @RequestParam(value = "selectClassValue", required = false, defaultValue = "-1") Integer selectClassValue,
            @RequestParam(value = "userId", required = false, defaultValue = "-1") Integer userId) {
        Page<ContestVO> page = contestService.pageContest(current, size, userId, name, selectStatusValue, selectClassValue);
        return Result.succeed("查询竞赛成功", page);
    }

    /**
     * 用户查询竞赛简介
     *
     * @param contestId 竞赛ID
     * @return Result<Contest>
     * @author CK
     * @date 2021/4/21 19:09
     */
    @GetMapping("/contestById")
    public Result<Contest> contestById(@RequestParam(value = "contestId") Integer contestId,
                                       @RequestParam(value = "userId") Integer userId) {
        QueryWrapper<Contest> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("title", "start_time", "end_time", "description", "privilege")
                .eq("contest_id", contestId)
                .eq("defunct", "N");
        Contest contest = contestService.getOne(queryWrapper);
        Assert.notNull(contest, "没有查询到相应的竞赛或作业");
        QueryWrapper<ContestUser> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("contest_id", contestId).eq("user_id", userId);
        ContestUser contestUser = contestUserService.getOne(queryWrapper1);
        if (ObjectUtil.isNull(contestUser)) {
            contest.setExist(0);
        } else {
            contest.setExist(1);
        }
        return Result.succeed("查询竞赛简介成功", contest);
    }

    /**
     * 查询竞赛的排名
     *
     * @param contestId 竞赛ID
     * @return Result<ContestRankVO>
     * @author CK
     * @date 2021/4/25 11:34
     */
    @GetMapping("/contestRank")
    public Result<ContestRankVO> contestRank(@RequestParam(value = "contestId") Integer contestId) {
        ContestRankVO contestRankVO = contestService.contestRank(contestId);
        return Result.succeed("查询竞赛排名成功", contestRankVO);
    }

}

